{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "输入某年某月某日，判断这一天是这一年的第几天？\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2024年12月1日是这一年的第336天\n"
     ]
    }
   ],
   "source": [
    "def is_leap_year(year):\n",
    "    \"\"\"判断是否为闰年\"\"\"\n",
    "    return (year % 4 == 0 and year % 100!= 0) or year % 400 == 0\n",
    "\n",
    "\n",
    "def day_of_year(year, month, day):\n",
    "    \"\"\"计算这一天是这一年的第几天\"\"\"\n",
    "    days_of_month = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "    if is_leap_year(year):\n",
    "        days_of_month[2] = 29\n",
    "    total_days = 0\n",
    "    for m in range(1, month):\n",
    "        total_days += days_of_month[m]\n",
    "    total_days += day\n",
    "    return total_days\n",
    "\n",
    "\n",
    "year = int(input(\"请输入年份: \"))\n",
    "month = int(input(\"请输入月份: \"))\n",
    "day = int(input(\"请输入日期: \"))\n",
    "\n",
    "result = day_of_year(year, month, day)\n",
    "print(f\"{year}年{month}月{day}日是这一年的第{result}天\")\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "判断1000中有多少个素数，并输出所有素数。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1000以内的素数有 168 个\n",
      "这些素数分别是：\n",
      "2\n",
      "3\n",
      "5\n",
      "7\n",
      "11\n",
      "13\n",
      "17\n",
      "19\n",
      "23\n",
      "29\n",
      "31\n",
      "37\n",
      "41\n",
      "43\n",
      "47\n",
      "53\n",
      "59\n",
      "61\n",
      "67\n",
      "71\n",
      "73\n",
      "79\n",
      "83\n",
      "89\n",
      "97\n",
      "101\n",
      "103\n",
      "107\n",
      "109\n",
      "113\n",
      "127\n",
      "131\n",
      "137\n",
      "139\n",
      "149\n",
      "151\n",
      "157\n",
      "163\n",
      "167\n",
      "173\n",
      "179\n",
      "181\n",
      "191\n",
      "193\n",
      "197\n",
      "199\n",
      "211\n",
      "223\n",
      "227\n",
      "229\n",
      "233\n",
      "239\n",
      "241\n",
      "251\n",
      "257\n",
      "263\n",
      "269\n",
      "271\n",
      "277\n",
      "281\n",
      "283\n",
      "293\n",
      "307\n",
      "311\n",
      "313\n",
      "317\n",
      "331\n",
      "337\n",
      "347\n",
      "349\n",
      "353\n",
      "359\n",
      "367\n",
      "373\n",
      "379\n",
      "383\n",
      "389\n",
      "397\n",
      "401\n",
      "409\n",
      "419\n",
      "421\n",
      "431\n",
      "433\n",
      "439\n",
      "443\n",
      "449\n",
      "457\n",
      "461\n",
      "463\n",
      "467\n",
      "479\n",
      "487\n",
      "491\n",
      "499\n",
      "503\n",
      "509\n",
      "521\n",
      "523\n",
      "541\n",
      "547\n",
      "557\n",
      "563\n",
      "569\n",
      "571\n",
      "577\n",
      "587\n",
      "593\n",
      "599\n",
      "601\n",
      "607\n",
      "613\n",
      "617\n",
      "619\n",
      "631\n",
      "641\n",
      "643\n",
      "647\n",
      "653\n",
      "659\n",
      "661\n",
      "673\n",
      "677\n",
      "683\n",
      "691\n",
      "701\n",
      "709\n",
      "719\n",
      "727\n",
      "733\n",
      "739\n",
      "743\n",
      "751\n",
      "757\n",
      "761\n",
      "769\n",
      "773\n",
      "787\n",
      "797\n",
      "809\n",
      "811\n",
      "821\n",
      "823\n",
      "827\n",
      "829\n",
      "839\n",
      "853\n",
      "857\n",
      "859\n",
      "863\n",
      "877\n",
      "881\n",
      "883\n",
      "887\n",
      "907\n",
      "911\n",
      "919\n",
      "929\n",
      "937\n",
      "941\n",
      "947\n",
      "953\n",
      "967\n",
      "971\n",
      "977\n",
      "983\n",
      "991\n",
      "997\n"
     ]
    }
   ],
   "source": [
    "\n",
    "def is_prime(num):\n",
    "    if num < 2:\n",
    "        return False\n",
    "    for i in range(2, int(num ** 0.5) + 1):\n",
    "        if num % i == 0:\n",
    "            return False\n",
    "    return True\n",
    "\n",
    "prime_numbers = []\n",
    "for n in range(2, 1000):\n",
    "    if is_prime(n):\n",
    "        prime_numbers.append(n)\n",
    "\n",
    "print(\"1000以内的素数有\", len(prime_numbers), \"个\")\n",
    "print(\"这些素数分别是：\")\n",
    "for prime in prime_numbers:\n",
    "    print(prime)\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "利用条件运算符的嵌套来完成此题：学习成绩>=90分的同学用A表示，60-89分之间的用B表示，60分以下的用C表示。（要求有键盘输入）\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你的成绩等级为: A\n"
     ]
    }
   ],
   "source": [
    "score = float(input(\"请输入你的学习成绩: \"))\n",
    "grade = \"\"\n",
    "if score >= 90:\n",
    "    grade = \"A\"\n",
    "elif score >= 60:\n",
    "    grade = \"B\"\n",
    "else:\n",
    "    grade = \"C\"\n",
    "\n",
    "print(\"你的成绩等级为:\", grade)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "输入一行字符串，分别统计出其中英文字母、空格、数字和其它字符的个数。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "英文字母个数: 4\n",
      "空格个数: 0\n",
      "数字个数: 3\n",
      "其它字符个数: 0\n"
     ]
    }
   ],
   "source": [
    "s = input(\"请输入一行字符串: \")\n",
    "letter_count = 0\n",
    "space_count = 0\n",
    "digit_count = 0\n",
    "other_count = 0\n",
    "\n",
    "for char in s:\n",
    "    if char.isalpha():\n",
    "        letter_count += 1\n",
    "    elif char.isspace():\n",
    "        space_count += 1\n",
    "    elif char.isdigit():\n",
    "        digit_count += 1\n",
    "    else:\n",
    "        other_count += 1\n",
    "\n",
    "print(\"英文字母个数:\", letter_count)\n",
    "print(\"空格个数:\", space_count)\n",
    "print(\"数字个数:\", digit_count)\n",
    "print(\"其它字符个数:\", other_count)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "一个数如果恰好等于它的因子之和，这个数就称为“完数”。例如6=1＋2＋3.编程，找出1000以内的所有完数。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "6\n",
      "28\n",
      "496\n"
     ]
    }
   ],
   "source": [
    "for num in range(1, 1000):\n",
    "    factor_sum = 0\n",
    "    for factor in range(1, int(num ** 0.5) + 1):\n",
    "        if num % factor == 0:\n",
    "            factor_sum += factor\n",
    "            another_factor = num // factor\n",
    "            if another_factor!= factor and another_factor < num:\n",
    "                factor_sum += another_factor\n",
    "    if factor_sum == num:\n",
    "        print(num)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "输入任意数N，求1+2!+3!+...+N!的值（注意实数范围）\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1+2!+3!+...+N!的值为: 122342346998826717539665299944651784048588130840420940313\n"
     ]
    }
   ],
   "source": [
    "N = int(input(\"请输入一个整数N: \"))\n",
    "result = 0\n",
    "factorial = 1\n",
    "for n in range(1, N + 1):\n",
    "    factorial *= n\n",
    "    result += factorial\n",
    "print(\"1+2!+3!+...+N!的值为:\", result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "输入任意奇数N，按如下要求打印出图案（菱形）\n",
    "    *\n",
    "  ***\n",
    " *****\n",
    "*******（N控制行数和本行的星数）\n",
    " *****\n",
    "  ***\n",
    "   *\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   *   \n",
      "  ***  \n",
      " ***** \n",
      "*******\n",
      " ***** \n",
      "  ***  \n",
      "   *   \n"
     ]
    }
   ],
   "source": [
    "N = int(input(\"请输入一个奇数N: \"))\n",
    "# 上半部分\n",
    "for i in range((N + 1) // 2):\n",
    "    print(\" \" * ((N - (2 * i + 1)) // 2), end=\"\")\n",
    "    print(\"*\" * (2 * i + 1), end=\"\")\n",
    "    print(\" \" * ((N - (2 * i + 1)) // 2))\n",
    "# 下半部分\n",
    "for i in range((N - 1) // 2):\n",
    "    print(\" \" * (i + 1), end=\"\")\n",
    "    print(\"*\" * (N - 2 * (i + 1)), end=\"\")\n",
    "    print(\" \" * (i + 1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "对10个数进行排序。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请依次输入10个数字：\n",
      "排序后的数字为： [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]\n"
     ]
    }
   ],
   "source": [
    "nums = []\n",
    "print(\"请依次输入10个数字：\")\n",
    "for _ in range(10):\n",
    "    num = float(input())\n",
    "    nums.append(num)\n",
    "\n",
    "for i in range(len(nums) - 1):\n",
    "    min_index = i\n",
    "    for j in range(i + 1, len(nums)):\n",
    "        if nums[j] < nums[min_index]:\n",
    "            min_index = j\n",
    "    if min_index!= i:\n",
    "        nums[i], nums[min_index] = nums[min_index], nums[i]\n",
    "\n",
    "print(\"排序后的数字为：\", nums)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "有一个已经排好序的数组（元素数量大于10）。从键盘任意输入一个数，要求按原来的规律将它插入数组中。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始已排好序的数组: [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21]\n",
      "插入后的数组: [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 46]\n"
     ]
    }
   ],
   "source": [
    "sorted_array = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21]\n",
    "print(\"原始已排好序的数组:\", sorted_array)\n",
    "\n",
    "num_to_insert = int(input(\"请输入一个要插入的数: \"))\n",
    "\n",
    "insert_index = 0\n",
    "for index in range(len(sorted_array)):\n",
    "    if num_to_insert <= sorted_array[index]:\n",
    "        insert_index = index\n",
    "        break\n",
    "    else:\n",
    "        insert_index = len(sorted_array)\n",
    "\n",
    "sorted_array.insert(insert_index, num_to_insert)\n",
    "\n",
    "print(\"插入后的数组:\", sorted_array)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "有n个人围成一圈，顺序排号。从第一个人开始报数（从1到3报数），凡报到3的人退出圈子，问最后留下的是原来第几号的那位。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最后留下的是原来第88号的那位\n"
     ]
    }
   ],
   "source": [
    "n = int(input(\"请输入总人数n: \"))\n",
    "people = list(range(1, n + 1))  # 初始化人员编号列表\n",
    "index = 0  # 报数的索引\n",
    "count = 0  # 当前报数的数字\n",
    "while len(people) > 1:\n",
    "    index %= len(people)  # 防止索引越界，循环使用索引\n",
    "    count += 1\n",
    "    if count % 3 == 0:\n",
    "        people.pop(index)  # 报到3的人退出圈子\n",
    "    else:\n",
    "        index += 1\n",
    "print(\"最后留下的是原来第{}号的那位\".format(people[0]))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
